import {Component, OnInit} from '@angular/core';
import {CooperationService} from '../cooperation.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {NzModalService} from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-cooperation-cooperation',
  templateUrl: './cooperation-cooperation.component.html',
  styleUrls: ['./cooperation-cooperation.component.scss']
})
export class CooperationCooperationComponent implements OnInit {
  query = {
    // 用户名
    name: '',
    // 当前页
    current: 1,
    // 分页大小
    size: 20,
  };
  listOfData = {
    current: 1,
    total: 100,
    size: 20,
    records: []
  };
  modal = {
    modalVisible: false,
    operationType: '',
    loading: false,
  };
  formData = {
    id: '',
    realName: '',
    phone: '',
    userName: '',
    passWord: '',
    repeatPassword: '',
    productIds: [],
  };
  appOptions: any[] = [];

  constructor(
    private cooperationService: CooperationService,
    private nzMessageService: NzMessageService,
    private nzModalService: NzModalService,
  ) {
  }

  ngOnInit(): void {
    this.queryList();
    this.cooperationService.getApp(1, 9999, '').then((res: any) => {
      if (res.code === 0) {
        this.appOptions = res.data.records;
      }
    });
  }

  queryBtn() {
    this.query.current = 1;
    this.queryList();
  }

  resetQuery() {
    this.query = {
      name: '',
      current: 1,
      size: 20,
    };
    this.queryList();
  }

  // 查询列表
  queryList(): void {
    this.cooperationService.getPartner(
      this.query.current,
      this.query.size,
      this.query.name
    ).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.current = index;
    this.queryList();
  }

  onPageSizeChange(size: number) {
    this.query.size = size;
    this.queryList();
  }

  // 添加
  add() {
    this.modal.modalVisible = true;
    this.modal.operationType = '添加';
  }

  // 查看
  view(data: any) {
    this.modal.modalVisible = true;
    this.modal.operationType = '查看';
    this.formData = data;
  }


  // 编辑
  edit(data: any) {
    this.modal.modalVisible = true;
    this.modal.operationType = '编辑';
    this.formData = JSON.parse(JSON.stringify(data));
    // @ts-ignore
    // tslint:disable-next-line:radix
    this.formData.productIds = this.formData.productIds.split(',').map(v => parseInt(v));
  }


  // 重置密码
  resetPassword(data: any) {
    const s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    let password = '';
    while (password.length < 8) {
      password += s[Math.floor(Math.random() * s.length)];
    }
    this.nzModalService.confirm({
      nzTitle: '警告',
      nzContent: `<b>确定要将密码重置为：${password}</b>`,
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: () => {
        this.cooperationService.resetPassword(data.id, password).then((res: any) => {
          if (res.code === 0) {
            this.nzMessageService.success('操作成功');
          }
        });
      },
      nzCancelText: '取消',
      nzOnCancel: () => {
      }
    });
  }

  // 模态框取消按钮
  handleModalCancel() {
    this.modal.modalVisible = false;
    this.resetFormData();
  }

  // 模态框确认按钮监听
  handleModalOk() {
    if (!this.formData.realName) {
      this.nzMessageService.warning('请输入姓名');
      return;
    }
    if (!this.formData.phone) {
      this.nzMessageService.warning('请输入手机号');
      return;
    }
    if (!this.formData.userName) {
      this.nzMessageService.warning('请输入用户名');
      return;
    }
    if (this.modal.operationType === '添加') {
      if (!this.formData.passWord) {
        this.nzMessageService.warning('请输入密码');
        return;
      }
      if (!this.formData.repeatPassword) {
        this.nzMessageService.warning('请再次输入密码');
        return;
      }
      if (this.formData.passWord !== this.formData.repeatPassword) {
        this.nzMessageService.warning('两次输入的密码不相同');
        return;
      }
    }
    this.modal.loading = true;
    this.cooperationService.addOrModifyPartner(this.formData, this.modal.operationType === '添加').then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.queryList();
      }
    }).finally(() => {
      this.resetFormData();
    });
  }

  // 重置数据
  resetFormData() {
    this.formData = {
      id: '',
      realName: '',
      phone: '',
      userName: '',
      passWord: '',
      repeatPassword: '',
      productIds: [],
    };
    this.modal = {
      modalVisible: false,
      operationType: '',
      loading: false,
    };
  }

  // 删除
  deleteRow(id: any) {
    this.cooperationService.deletePartner(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryList();
      }
    });
  }
}
